import axios from "axios";
import { nanoid } from "nanoid";
import { defineStore } from "pinia";

export const useTalkStore = defineStore("talk", {
  actions: {
    async getATalk() {
      try {
        // 接收请求成功返回的数据并进行解构获取最终数据
        const {
          data: { content: title }, // 多层解构并将content进行重命名
        } = await axios.get(
          "https://api.uomg.com/api/rand.qinghua?format=json"
        );
        // 将请求成功的数据包装成一个对象
        const talk = {
          id: nanoid(), // 使用nanoid用于生成唯一id
          title,
        };
        // 将数据添加到数组中
        this.talkList.unshift(talk);
      } catch (error) {
        console.warn(error);
      }
    },
  },
  // 存储数据
  state() {
    return {
      talkList: [
        {
          id: "tw001",
          title: "今天你有点怪，哪里怪？怪好看的！",
        },
      ],
    };
  },
});
